/*
 * Copyrignt (c) xuzeshui.com. All Rights Reserved.
 * Author: Zeshui Xu<i@xuzeshui.com>
 * Created Time: 2015-08-22 14:38:49
 * Last Modified: 2017-08-10 16:10:42
 * File Name: auth/appkey.go
 * Description:
 */
package auth

import (
//"sync"
)

var (
	authAppKey *authAppKeyService
)

//每个appkey的基本信息
type AppKeyItem struct {
	Desc          string //appkey的描述
	SecretKey     string //私钥
	Plat          int8   //平台来源
	AccessTokenId int    //ak分组标识,不同分组ak可同时存在
}

func newAppKeyItem(desc, secrect string, plat int8, akid int) *AppKeyItem {
	return &AppKeyItem{
		Desc:          desc,
		SecretKey:     secrect,
		Plat:          plat,
		AccessTokenId: akid,
	}
}

type authAppKeyService struct {
	appKeyInfo map[string]*AppKeyItem
	//appkeyInfoLock *sync.RWMutex
}

/*
	构造authAppKeyService实例
*/
func newAuthAppKeyService() *authAppKeyService {
	service := &authAppKeyService{
		appKeyInfo: make(map[string]*AppKeyItem),
		//appkeyInfoLock: new(sync.RWMutex),
	}
	service.initAppKeyInfo()
	return service
}

func GetAuthAppKeyService() *authAppKeyService {
	if authAppKey == nil {
		authAppKey = newAuthAppKeyService()
	}
	return authAppKey
}

//初始化加载配置,当前为硬编码
func (p *authAppKeyService) initAppKeyInfo() {
	p.appKeyInfo["140a6d51d67c477e83cd91a241678824"] = newAppKeyItem("后台服务", "88366f9fad954828a194f3232a78a471", 0, 0)
	p.appKeyInfo["ff983cecc76d406a8b1c7c3c1b7cd172"] = newAppKeyItem("fingertip android", "passport.app.qkier.com", 1, 1)
	p.appKeyInfo["cc0dec8c07444e9dadb75d41218c4fe4"] = newAppKeyItem("fingertip ios", "passport.app.qkier.com", 2, 1)
	p.appKeyInfo["27a317d8489d40809572a5af28630e94"] = newAppKeyItem("tv passport", "65a5e88bd43c43c49e62bf307e4f2071", 3, 2)
	p.appKeyInfo["40b3409b84ce4177b30056605e36e785"] = newAppKeyItem("ixi", "1cf30665b408425d83655205909e8239", 4, 3)
	//p.appKeyInfo["5609e3f2e6ab48c6a9b508be41e96b31"] = newAppKeyItem("qiwo", "893afdafd12b40508ffe9c66c8df9195", 6, 4)
	p.appKeyInfo["f3533bd72a894282ba3296e9291d77dd"] = newAppKeyItem("qunxin", "e166f9723e8143d3a845eddc9c45a4f8", 7, 5)
	p.appKeyInfo["cc4477d042f2473da5d418bfd9730efa"] = newAppKeyItem("weijia ios", "4fada84faad540ccb58116c1f17b7819", 8, 6)
	p.appKeyInfo["271d8700b1074a9582a8cc4cdff94c5f"] = newAppKeyItem("qingke android", "9c9babfb8a3f4bb48d4ea651368585f2", 9, 7)
	p.appKeyInfo["0272050d21d641e2b6f188fde6e48476"] = newAppKeyItem("qingke ios", "4a25a6e2719648789640f9254dd862a9", 10, 7)
	//p.appKeyInfo["2423a25a73d04f5fba03f1ee97059fb1"] = newAppKeyItem("skyshop", "91a2b83d23324cde953958251469bf7f", 11, 8)
	p.appKeyInfo["8d4a3664e2ef422e978f4dd044cebb81"] = newAppKeyItem("zhsq", "97ab372042864e8e8dbed22e00d229e4", 12, 9) //群欣测试
	p.appKeyInfo["4e048052beba452e8f3f5358b67e3ef5"] = newAppKeyItem("weijia android", "d27980148280492e8ef1d7f131a0284a", 13, 6)
	p.appKeyInfo["19854c206c2f4a82b33ce98872ab31aa"] = newAppKeyItem("qk league", "eb90e35a31b63415f717482d209f8aa2", 14, 0)
	p.appKeyInfo["81d5f8b4e09c401da925d1a3391a3386"] = newAppKeyItem("h5", "971e9b9d92ae49f9bfbff5e0d5078fe1", 15, 11)
	p.appKeyInfo["e3b01549970b4a65a3b29f8e2c12939b"] = newAppKeyItem("qingke wechat h5", "ca1ffef7a98a42f6b2042fe8ce82fc73", 16, 12)
	p.appKeyInfo["a5140edb91d69e93b47a6d84dac56740"] = newAppKeyItem("pad ice wash", "d7c11074e5ea78e0633a806636aaf26b", 17, 0)
}

//验证appkey是否有效
func (p *authAppKeyService) ValidateAppKey(appkey string) bool {
	_, ok := p.GetAppKeyItem(appkey)
	return ok
}

//根据appkey获取appkey的基本信息
func (p *authAppKeyService) GetAppKeyItem(appkey string) (*AppKeyItem, bool) {
	item, ok := p.appKeyInfo[appkey]
	return item, ok
}

//获取appkey对应的私钥
func (p *authAppKeyService) GetSecretKey(appkey string) (string, bool) {
	item, ok := p.GetAppKeyItem(appkey)
	if !ok {
		return "", false
	}
	return item.SecretKey, true
}

/*
   获取平台信息
*/
func (p *authAppKeyService) GetPlat(appkey string) int8 {
	item, ok := p.GetAppKeyItem(appkey)
	if !ok {
		return 0
	}
	return item.Plat
}

/*
	获取
*/
func (p *authAppKeyService) GetAKId(appkey string) int {
	item, ok := p.GetAppKeyItem(appkey)
	if !ok {
		return 0
	}
	return item.AccessTokenId
}
